package com.easy.easy.Controller;

import com.easy.easy.Service.IAddressService;
import com.easy.easy.bean.Cart;
import com.easy.easy.bean.User;
import com.easy.easy.bean.Useraddress;
import com.easy.easy.util.LayData;
import com.easy.easy.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("address")
public class UserAddressController {

    @Autowired
    IAddressService addressService;
    @Autowired
    private View error;

    @RequestMapping("add")
    @ResponseBody
    public LayData add(HttpSession session , Useraddress address) {
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid=loginUser.getUserid();
        address.setUserid(userid);
        addressService.add(address);
        return LayData.success();
    }

    /**
     * 获取当前用户的所有地址
     */
    @RequestMapping("getaddressbyuserid")
    @ResponseBody
    public LayData getAddressByUserID(HttpSession session) {
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid = loginUser.getUserid();
        List<Useraddress> addresses = addressService.getAddressByUserID(userid);
        return LayData.success(addresses);
    }

    @RequestMapping("getdeaddressbyuserid")
    @ResponseBody
    public LayData getdeAddressByUserID(HttpSession session) {
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid = loginUser.getUserid();
        List<Useraddress> addresses = addressService.getAddressByUserID(userid);
        return LayData.success(addresses);
    }

    /**
     * 根据地址ID获取地址详情
     */
    @RequestMapping("getaddressbyid")
    @ResponseBody
    public LayData getAddressByID(int id) {
        Useraddress address = addressService.getAddressByID(id);
        if (address != null) {
            return LayData.success(address);
        } else {
            return LayData.success(404, "未找到该地址");
        }
    }

    /**
     * 更新地址
     */
    @RequestMapping("update")
    @ResponseBody
    public LayData update(HttpSession session, Useraddress address) {
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid = loginUser.getUserid();
        address.setUserid(userid);
        int result = addressService.edit(address);
        if (result > 0) {
            return LayData.success("地址更新成功");
        } else {
            return LayData.success(500, "地址更新失败");
        }
    }

    /**
     * 删除地址
     */
    @RequestMapping("delete")
    @ResponseBody
    public LayData delete(int id) {
        int result = addressService.delete(id);
        if (result > 0) {
            return LayData.success("地址删除成功");
        } else {
            return LayData.success(500, "地址删除失败");
        }
    }

    /**
     * 设置默认地址
     */
    @RequestMapping("setdefault")
    @ResponseBody
    public LayData setDefault(HttpSession session, int id) {
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid = loginUser.getUserid();
        // 取消当前用户的默认地址
        addressService.cancelDefault(userid);
        // 设置新的默认地址
        int result = addressService.setDefault(userid, id);
        if (result > 0) {
            return LayData.success("默认地址设置成功");
        } else {
            return LayData.success(500, "默认地址设置失败");
        }
    }

    @RequestMapping("canceldefault")
    @ResponseBody
    public LayData cancelDefault(int id) {
        // 调用服务层方法，取消该地址的默认状态
        int result = addressService.cancelDefault(id);

        if (result > 0) {
            return LayData.success("默认地址已取消");
        } else {
            return LayData.success(500, "取消默认地址失败");
        }
    }



}
